package com.netease.nim.uikit.session.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.cache.NimUserInfoCache;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

/**
 * Created by Administrator on 2017/9/22.
 */

public class UserInfoExtendHelper {


    public static final int ADMIN_BIG = 5; // 族长
    public static final int ADMIN_MID = 1; // 长老
    public static final int ADMIN_SMALL = 0; // 成员

//    public static UserInfoExtendHelper getInstance() {
//        return UserInfoExtendHelper.InstanceHolder.instance;
//    }
//
//    static class InstanceHolder {
//        final static UserInfoExtendHelper instance = new UserInfoExtendHelper();
//    }

    private static int getUserLevel(String account){
        int level = 0;
        NimUserInfo userInfo = NimUserInfoCache.getInstance().getUserInfo(account);
        if (userInfo==null){
            return level;
        }
        try {
            JSONObject object = JSON.parseObject(userInfo.getExtension());
            if (object.containsKey("lv")){
                level = object.getIntValue("lv");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return level;
    }

    public static String getUserLevelTitle(String account){
        NimUserInfo userInfo = NimUserInfoCache.getInstance().getUserInfo(account);
        String level ="";
        if (userInfo==null){
            return level;
        }
        try {
            JSONObject object = JSON.parseObject(userInfo.getExtension());
            if (object.containsKey("title")){
                level = object.getString("title");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return level;
    }
    public static int  getUserAdmin(String account){
        NimUserInfo userInfo = NimUserInfoCache.getInstance().getUserInfo(account);
        int  admin = 0;
        if (userInfo==null){
            return admin;
        }
        try {
            JSONObject object = JSON.parseObject(userInfo.getExtension());
            if (object.containsKey("admin")){
                admin = object.getIntValue("admin");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return admin;
    }


    public static int getUserExtendLevelColor(String account) {
        int level = getUserLevel(account);
        int colorid = R.color.level_1;
        switch (level) {
            case 1:
                colorid = R.color.level_1;
                break;
            case 2:
            case 3:
                colorid = R.color.level_2_3;
                break;
            case 4:
            case 5:
                colorid = R.color.level_4_5;
                break;
            case 6:
            case 7:
                colorid = R.color.level_6_7;
                break;
            case 8:
            case 9:
                colorid = R.color.level_8_9;
                break;
            case 10:
                colorid = R.color.level_10;
                break;
        }
        return colorid;
    }

    /**
     * 获取职位
     *
     * @param account
     * @return
     */
    public static int getAdminRstrId(String account) {
        int strid = R.string.str_family_admin_0;
        switch (getUserAdmin(account)) {
            case ADMIN_BIG:
                strid = R.string.str_family_admin_5;
                break;
            case ADMIN_MID:
                strid = R.string.str_family_admin_1;
                break;
            case ADMIN_SMALL:
                strid = R.string.str_family_admin_0;
                break;
        }
        return strid;
    }

}
